<?php
/*
 * Copyright (c) 2011 Todd Brady
 *
 * PS3Theatre is freely distributable under the terms of the MIT license.
 * For details, see the MIT-Liscence web site: http://www.opensource.org/licenses/mit-license.php 
 */
 
	include("includes/db_connect.php");
	$seriesId = $_GET['seriesId'];
	$seriesName = $_GET['seriesName'];
	$seasonNum = $_GET['seasonNum'];
	$numEps = 1;
	$uid=$_GET['uId'];
	$h = $_GET['h'];
	$w = $_GET['w'];
	$cacheHttpDir = $_SESSION['cacheHttpDir'];
	$removeChars = array("'",'"');
	
	$seriesInfoResult=mysql_query("SELECT s.tvdbseries_id, s.description, s.genre, ci.subfolder_path, ci.filename FROM tbl_tvshow_series s LEFT OUTER JOIN tbl_cacheimage ci ON (s.tvdbseries_id = ci.link_id and ci.type_id=2 and ci.keyword='backdrop') WHERE s.id=$seriesId") or die(mysql_error());
	$seriesRow = mysql_fetch_array($seriesInfoResult);
	$epsQuery = "SELECT d.http_path, e.subfolder_path, e.filename, e.season, e.episode, e.name, e.description, s.tvdbseries_id, e.tvdbepisode_id, ci.subfolder_path as cache_subfolder_path, ci.filename as cache_filename FROM tbl_tvshow_episode e LEFT OUTER JOIN tbl_cacheimage ci ON (e.tvdbepisode_id = ci.link_id and ci.type_id=2 and ci.keyword='epScreenshot'), tbl_tvshow_series s, tbl_directories d WHERE d.id=e.directory_id AND s.id=e.series_id AND e.series_id=$seriesId";
	$epsWhere = "";
	$specialsWhere = " AND e.season=0 ORDER BY episode";
	if($seasonNum == 'all'){
		$epsWhere .= " AND e.season!=0 ORDER BY season,episode";
		$specialsResult=mysql_query($epsQuery.$specialsWhere) or die(mysql_error());
		$numEps = $numEps + mysql_num_rows($specialsResult);
	}else{
		$epsWhere .= " AND e.season=$seasonNum ORDER BY episode";
	}
	$epsResult=mysql_query($epsQuery.$epsWhere) or die(mysql_error());
	$numEps = $numEps + mysql_num_rows($epsResult);
			
	//variables for the top left info panel
	$leftTopLbl1 = 'TV Shows';
	$leftTopLbl2 = $seriesName;
	if($seasonNum > 0){
		$leftTopLbl3 = 'Season '.$seasonNum;
	}else if($seasonNum == '0'){
		$leftTopLbl3 = 'Specials';
	}else{
		$leftTopLbl3 = 'All Eps';
	}	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PS3 Theatre <?php echo $_SESSION['version'];?></title>
<script type="text/javascript" src="js/time.js"></script>
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<script type="text/javascript" src="js/animator.js"></script>
<script type="text/javascript" src="js/ui.js"></script>
<script type="text/javascript" src="js/tvShowsEps_ui.js"></script>
<script>
var epTableIndex = 1;
var numEps = <?php echo $numEps;?>;
seriesId = <?php echo $seriesId;?>;
tvdbSeriesId = <?php echo $seriesRow['tvdbseries_id'];?>;

function loadBackgroundArt() {
		new Ajax.Request('scripts/tvShowImport.php?seriesId='+tvdbSeriesId+'&backdrop=Y&uId='+getUniqueCode(),
		  {
			method:'get',
			onSuccess: function(transport){
				var json = transport.responseText.evalJSON();
				
				if(json.backdropSrc != ''){
					document.body.background=json.backdropSrc;
				}else{
					document.body.background='images/movies-background.png';
				}
			},
			onFailure: function(){ alert('error loading backdrop...'); }
		  });
	 }
	 
	 function selectEp(index) {
		var epDiv = $('ep'+index);

		if(index > 0)	 {
		 	$('epTitleSpan').innerHTML = '"'+epDiv.readAttribute('title')+'"';
			$('epNumSpan').innerHTML = 'SEASON '+epDiv.readAttribute('seasonNum')+'/EPISODE '+epDiv.readAttribute('epNum');
			$('epDescSpan').innerHTML = epDiv.readAttribute('description');
			$('epScreenshot').innerHTML = '<img src="'+epDiv.readAttribute('imageUrl')+'" width="850" height="485"/>';
		}else{
			$('epTitleSpan').innerHTML = '';
			$('epNumSpan').innerHTML = '';
			$('epDescSpan').innerHTML = '';
			$('epScreenshot').innerHTML = '';
		}
		
		$('epDivBorder'+index).removeClassName('epDivBorder');
		$('epDivBorder'+index).addClassName('epDivBorderSelected');
		
		$('h2'+index).removeClassName('epH2');
		$('h2'+index).addClassName('epH2Sel');
	}
	
	function unselectEp(index){
		$('epDivBorder'+index).removeClassName('epDivBorderSelected');
		$('epDivBorder'+index).addClassName('epDivBorder');
		
		$('h2'+index).removeClassName('epH2Sel');
		$('h2'+index).addClassName('epH2');
	}
	
	function loadAllImages(){
		var imgDivs = $$('div.loadable-image');
		var i=0;
		while(i<imgDivs.length){
			loadEpScreenshotArt(imgDivs[i],
						  imgDivs[i].readAttribute('index'),
						  imgDivs[i].readAttribute('tvdbEpId'),
  						  imgDivs[i].readAttribute('seasonId'),
						  imgDivs[i].readAttribute('epId'));
			i++;
		}
	}
	
	function loadEpScreenshotArt(hrefObj, index, tvdbEpId, seasonId, epId){
		new Ajax.Request('scripts/tvShowImport.php?seriesId='+tvdbSeriesId+'&tvdbEpId='+tvdbEpId+'&seasonId='+seasonId+'&epId='+epId+'&epScreenshot=Y&uId='+getUniqueCode(),
		  {
			method:'get',
			onSuccess: function(transport){
				var json = transport.responseText.evalJSON();
				if(json.epScreenshotSrc != ''){
					$('epImgSpan'+index).innerHTML = '<img id="epImg'+index+'" src="'+json.epScreenshotSrc+'" width="400" height="225" class="epImage"/>';
				}else{
					$('epImgSpan'+index).innerHTML = '<img id="epImg'+index+'" src="" width="400" height="225" class="epImage"/>';
				}
			
				hrefObj.removeClassName('loadable-image');
				hrefObj.addClassName('loaded');
			},
			onFailure: function(){ 
				alert('error loading ep screenshot...');
			}
		  });
	 }
</script>
<link href="css/main.css" rel="stylesheet" type="text/css">
<meta http-equiv="Pragma" content="no-cache"> 
</head>

<body bgcolor="#000000" background="<?php if($seriesRow['filename'] != ''){ echo $_SESSION['cacheHttpDir'].$seriesRow['subfolder_path'].$seriesRow['filename'];}?>" style="overflow:hidden;" onLoad="clock();<?php if($seriesRow['filename'] == ''){?>loadBackgroundArt(<?php echo $seriesRow['tvdbseries_id'];?>);<?php } ?>loadAllImages(); selectEp(1); updateScrollbar(0,1);">
<div class="wrapper" style="overflow:hidden;" >

<!-- top left info panel -->
<?php include('/includes/topLeftInfoPanel.php'); ?>

<!-- top right info panel -->
<?php include('/includes/topRightInfoPanel.php'); ?>

<!-- ep screenshot scroll panel-->
<div id="screenshotPanel" style="position:absolute; left:935px; top:75px; width:840px; height:935px; overflow:hidden;">
<table id="screenshotTable" width="800" cellspacing="5">
<tr>
<td>
<h2 id="h20" class="epH2">
<div class="loaded" 
	id="ep0"
	link="tvShowsSeasons.php?seriesId=<?php echo $seriesId;?>&seriesName=<?php echo $seriesName;?>&w=<?php echo $w;?>&h=<?php echo $h;?>&uId=<?php echo $uid;?>">

<span class="epOverlay"></span>

<span class="epDivLabel">..</span>

<span id="epDivBorder0" class="epDivBorder"></span>

<span id="epImgSpan0">
	<img id="epImg0" src="images/tvep-upposter.png" width="400" height="225" class="epImage"/>
</span>
</div>
</h2>
</td>
<?php
	$i = 1;
	$count = 1;
	while ($row = mysql_fetch_array($epsResult)) {
		$seasonText = $row['season'];
		if($seasonText < 10){
			$seasonText = '0'.$seasonText;
		}
		$episodeText = $row['episode'];
		if($episodeText < 10){
			$episodeText = '0'.$episodeText;
		}
		
	if($row['cache_filename'] != ''){
		$loaderClassName = 'loaded';
		$epScreenSrc = $_SESSION['cacheHttpDir'].$row['cache_subfolder_path'].$row['cache_filename'];
	}else{
		$loaderClassName = 'loadable-image';
		$epScreenSrc = 'images/ajax-loadertv.gif';	
	}
?>
<td>
<h2 id="h2<?php echo $count;?>" class="epH2">
<div class="<?php echo $loaderClassName;?>" 
		id="ep<?php echo $count;?>" 
		index="<?php echo $count;?>" 
		tvdbEpId="<?php echo $row['tvdbepisode_id'];?>" 
		seasonId="<?php echo $row['season'];?>" 
		epId="<?php echo $row['episode'];?>" 
		link="<?php echo $row['http_path'].$row['subfolder_path'].$row['filename'];?>" 
		title="<?php echo trim(str_replace($removeChars,"", $row['name']));?>" 
		seasonNum="<?php echo $row['season']; ?>"
		epNum="<?php echo $row['episode']; ?>"
		description="<?php echo trim(str_replace($removeChars,"", $row['description']));?>"
		imageUrl="<?php echo $cacheHttpDir.'TV Shows/'.$row['tvdbseries_id'].'-epScreenshot'.$seasonText.$episodeText.'.jpg';?>">
<span class="epOverlay"></span>

<span class="epDivLabel"><?php echo 'S'.$seasonText.'E'.$episodeText.'. '.$row['name'];?></span>

<span id="epDivBorder<?php echo $count;?>" class="epDivBorder"></span>

<span id="epImgSpan<?php echo $count;?>">
	<img id="epImg<?php echo $count;?>" src="<?php echo $epScreenSrc;?>" width="400" height="225" class="epImage"/>
</span>
</div>
</h2>
</td>
<?php
$i++;
$count++;
	if($i == 2){
		echo '</tr><tr>';
		$i=0;
	}
}

if($seasonNum == 'all'){
while ($row = mysql_fetch_array($specialsResult)) {
		$seasonText = $row['season'];
		if($seasonText < 10){
			$seasonText = '0'.$seasonText;
		}
		$episodeText = $row['episode'];
		if($episodeText < 10){
			$episodeText = '0'.$episodeText;
		}
		
		if($row['cache_filename'] != ''){
			$loaderClassName = 'loaded';
			$epScreenSrc = $_SESSION['cacheHttpDir'].$row['cache_subfolder_path'].$row['cache_filename'];
		}else{
			$loaderClassName = 'loadable-image';
			$epScreenSrc = 'images/ajax-loadertv.gif';	
		}
?>
<td>
<div>
<h2 id="h2<?php echo $count;?>" class="epH2">
<div class="<?php echo $loaderClassName;?>" 
		id="ep<?php echo $count;?>" 
		index="<?php echo $count;?>" 
		tvdbEpId="<?php echo $row['tvdbepisode_id'];?>" 
		seasonId="<?php echo $row['season'];?>" 
		epId="<?php echo $row['episode'];?>" 
		link="<?php echo $row['http_path'].$row['subfolder_path'].$row['filename'];?>" 
		title="<?php echo trim(str_replace($removeChars,"", $row['name']));?>" 
		seasonNum="<?php echo $row['season']; ?>"
		epNum="<?php echo $row['episode']; ?>"
		description="<?php echo trim(str_replace($removeChars,"", $row['description']));?>"
		imageUrl="<?php echo $cacheHttpDir.'TV Shows/'.$row['tvdbseries_id'].'-epScreenshot'.$seasonText.$episodeText.'.jpg';?>">
<span class="epOverlay"></span>

<span class="epDivLabel"><?php echo 'S'.$seasonText.'E'.$episodeText.'. '.$row['name'];?></span>

<span id="epDivBorder<?php echo $count;?>" class="epDivBorder"></span>

<span id="epImgSpan<?php echo $count;?>">
	<img id="epImg<?php echo $count;?>" src="<?php echo $epScreenSrc;?>" width="400" height="225" class="epImage"/>
</span>
</div>
</h2>
</div>
</td>
<?php
$i++;
$count++;
	if($i == 2){
		echo '</tr><tr>';
		$i=0;
	}
}
}
?>
</tr>
</table>
</div>

<!-- ep big screenshot-->
<div id="epScreenshot" style="position:absolute; left:55px; top:105px; width:825px; z-index:1; text-align:center;">
<img src="" width="850" height="485"/>
</div>

<!-- ep text div-->
<div style="position:absolute; left:55px; top:620px; width:825px; overflow:hidden; z-index:1;">
<center>
<span id="epTitleSpan" class="epTitleSpan"></span><br/>
<span id="epNumSpan" class="epNumSpan"></span><br/>
</center>
<span id="epDescSpan" class="epDescSpan"></span>
</div>

<!-- scrollbar back -->
<div style="position:absolute; left:1766px; top:85px; z-index:6;">
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><img src="images/scrollbarback_vertical_top.png" height="22" width="17"/></td>
  </tr>
  <tr>
    <td style="height:865px; background:url(images/scrollbarback_vertical_middle.png) repeat-y;"></td>
  </tr>
  <tr>
    <td><img src="images/scrollbarback_vertical_bottom.png" height="22" width="17"/></td>
  </tr>
</table>
</div>

<!-- scrollbar nub -->
<div id="scrollNub" style="position:absolute; left:1768px; top:104px; z-index:6;" class="scrollNub">
</div>

<!-- overlay -->
<div class="displayOverlay"></div>

<!-- Modal Window Code -->
<?php include('/includes/modalWindow.php'); ?>
</body>
</html>
